﻿https://leetcode.cn/problems/single-number-ii/description/
//我的
class Solution {
public:
    int singleNumber(vector<int>& nums) {
        vector<int> count(32, 0);

        for (auto& num : nums) {
            for (int i = 0; i < 32; i++) {
                if (num & (1 << i))count[i]++;
            }
        }

        int ret = 0;
        for (int i = 0; i < 32; i++) {
            ret |= (count[i] % 3) << i;
        }
        return ret;
    }
};

//答案
class Solution
{
public:
    int singleNumber(vector<int>& nums)
    {
        int ret = 0;
        for (int i = 0; i < 32; i++) // 依次去修改 ret 中的每⼀位
        {
            int sum = 0;
            for (int x : nums) // 计算nums中所有的数的第 i 位的和
                if (((x >> i) & 1) == 1)
                    sum++;
            sum %= 3;
            if (sum == 1) ret |= 1 << i;
        }
        return ret;
    }
};